import Vue from 'vue'
import VueRouter from 'vue-router'
import MyLogin from '../components/MyLogin.vue'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/home' },
  { path: '/login', component: MyLogin },
  {
    path: '/home',
    component: () => import('../components/MyHome.vue'),
    children: [
      {
        path: 'users',
        component: () => import('../components/menus/MyUsers.vue')
      },
      {
        path: 'rights',
        component: () => import('../components/menus/MyRights.vue')
      },
      {
        path: 'orders',
        component: () => import('../components/menus/MyOrders.vue')
      },
      {
        path: 'goods',
        component: () => import('../components/menus/MyGoods.vue')
      },
      {
        path: 'settings',
        component: () => import('../components/menus/MySettings.vue')
      },
      {
        path: 'detail/:id',
        component: () => import('../components/user/MyUserDetail.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  //to 到哪里去   to.path 表示即将要访问的那个地址
  //from 从哪里来
  //next() -- 表示放行   next(false)  -- 不允许通过    next('/login) --- 强行跳转到login
  if (to.path === '/login') {
    next()
  } else {
    //访问的不是登录页,需要判断有没有token(有token说明登录过：没有token说明退出了或没有登录)
    if (localStorage.getItem('token')) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
